/* eslint-disable import/no-anonymous-default-export */
// 引入混合类型
import {FETCH_RECOMMEND_PRODUCTS_FAIL, FETCH_RECOMMEND_PRODUCTS_START, FETCH_RECOMMEND_PRODUCTS_SUCCESS, RecommendProductAction} from './recommendProductsActions'

interface RecommendProductsState {
  productList: [],
  loading: Boolean,
  error: string | Number | null
}

const defaultState: RecommendProductsState = {
  loading: true,
  error: null,
  productList: []
}
// 使用混合类型定义action
export default (state = defaultState, action: RecommendProductAction) => {
  switch(action.type) {
    case FETCH_RECOMMEND_PRODUCTS_START:
      // 其余数据保持不变,只有loading变化
      return {...state, loading: true}
    case FETCH_RECOMMEND_PRODUCTS_SUCCESS:
      return {...state, loading: false, productList: action.value}
    case FETCH_RECOMMEND_PRODUCTS_FAIL:
      return {...state, loading: false, error: action.value}
    default: 
      return state
  }
}